package com.etc.controller;



import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.Desk;
import com.etc.entity.FoodBean;
import com.etc.entity.FoodType;
import com.etc.entity.LineupBean;
import com.etc.entity.Order;
import com.etc.service.FoodService;

@Controller
@RequestMapping(value="food")
public class FoodController {
	@Autowired
	private FoodService fService;
	@RequestMapping(value="list",method = RequestMethod.GET)
	public String list(Model model) {
		List<FoodBean> list1=fService.getFood(1, 1);
		List<FoodBean> list2=fService.getFood(1, 2);
		List<FoodBean> list3=fService.getFood(1, 3);
		
		model.addAttribute("list1",list1);
		model.addAttribute("list2",list2);
		model.addAttribute("list3",list3);
		return "forward:/shop.jsp";
		
		
	}
	@RequestMapping(value="getdesk",method=RequestMethod.GET)
	@ResponseBody
	public List<Desk> deskGet() {
		
		List<Desk> list=fService.getTable(1);
		return list;
		
		
	}
	//添加订单的事务
	@RequestMapping(value="order")
	public String oder(String nums,String totalPrice,String ids,String deskId) {
	Order order=new Order(Integer.valueOf(deskId), Integer.valueOf(totalPrice), 1);
	
	
	ids = ids.substring(0, ids.length() - 1);
	String id[] = ids.split(",");
	nums = nums.substring(0, nums.length() - 1);
	String num[] = nums.split(",");
		boolean flog=fService.addBoth(order, id, num);
		boolean flag=fService.doUpDesk1(Integer.valueOf(deskId));
		return "redirect:list";
	}
	
	@RequestMapping(value="getFoodType",method=RequestMethod.GET)
	@ResponseBody
	public List<FoodType> getType() {
	
		List<FoodType> list=fService.getFoodType();
				
		return list;		
		
	}
	
	@RequestMapping(value="getAddFood")
	public String getFood() {
		return "addFood";
	}
	
	
	
	
	@RequestMapping(value="addFood",method = RequestMethod.GET)
	public String addFoodType(HttpServletRequest request,HttpServletResponse response)
	{	
		FoodBean food=new FoodBean();
		//获取菜品的名字，价格，类型编号，图片路径
		String foodName=request.getParameter("foodName");
		double foodPrice=Double.valueOf(request.getParameter("foodPrice"));
		int foodTypeId=Integer.valueOf(request.getParameter("foodType"));
		String foodPic="images/"+request.getParameter("foodPic");
		
		if("images/".equals(foodPic)) {
			foodPic="";
		}
		
		food.setFoodName(foodName);
		food.setFoodPrice(foodPrice);
		food.setFoodPic(foodPic);
		food.setFoodTypeId(foodTypeId);
		
		boolean flag=fService.insertFood(food);
		//model.addAttribute("FoodBean", food);
		
		return "addFood";
	}
	@RequestMapping(value="getlineup",method=RequestMethod.GET)
	@ResponseBody
	public int getlineup() {
		//获取第一家点的当前号码
		List<LineupBean> list=fService.gesMinLineup(1);
		return list.get(0).getLineupNo();
	}
	@RequestMapping(value="updatelineup",method=RequestMethod.GET)
	@ResponseBody
	public int getupdatelineup() {
		//获取第一家点的当前号码
		List<LineupBean> list=fService.gesMinLineup(1);
		//修改牌号的状态
		int a=fService.doUpdateLine(list.get(0).getLineupId());
		//获取最新的叫号号码
		List<LineupBean> list1=fService.gesMinLineup(1);
		return list1.get(0).getLineupNo();
	}
}
